package edu.jkd.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import edu.jkd.entity.Customer;
import edu.jkd.entity.Flow;
import edu.jkd.entity.Myorder;
import edu.jkd.mapper.FlowMapper;
import edu.jkd.mapper.MyorderMapper;
import edu.jkd.service.IMyorderService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import edu.jkd.util.Result;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author lsk
 * @since 2022-11-23
 */
@Service
public class MyorderServiceImpl extends ServiceImpl<MyorderMapper, Myorder> implements IMyorderService {
    @Resource
    private MyorderMapper myorderMapper;

    @Resource
    private FlowMapper flowMapper;

    @Override
    public boolean updateStatus(Integer orderId, String tradeNo, BigDecimal payPrice) {
        //添加流水
        Flow flow =  new Flow();
        flow.setFlowNumber(tradeNo);
        flow.setOrderId(orderId);
        flow.setCustId(2);
        flow.setPayPrice(payPrice);
        flow.setPayTime(LocalDateTime.now());
        flowMapper.insert(flow);

//修改订单状态
        Myorder myorder = new Myorder();
        myorder.setOrderId(orderId);
        myorder.setStatus(2);
        int rows =  myorderMapper.updateById(myorder);
        return rows >0;
    }
    /**
     *
     * @param id
     * @return
     */
    @Override
    public Result deleteOrderById(String[] id) {
        Result result = new Result();
        List<Integer> list = Arrays.stream(id).map(Integer::valueOf).
                collect(Collectors.toList());
        System.out.println(list);

            int num = myorderMapper.deleteBatchIds(list);
            if (num!=0){
                result.setCode("1");
                result.setMessage("成功");
            }else {
                result.setCode("0");
                result.setMessage("失败");
            }


        return result;
    }
    /**
     * 实现分页功能
     * @param currentPage 当前页码
     * @param pageSize 显示条数
     * @return
     */
    @Override
    public List<Myorder> selectByPage(Integer currentPage, Integer pageSize,Integer cutsId) {


        LambdaQueryWrapper<Myorder> qw = new LambdaQueryWrapper<>();
        Page<Myorder> page = new Page<>(currentPage,pageSize);
        qw.eq(Myorder::getCustId,cutsId);
        Page<Myorder> iPage = myorderMapper.selectPage(page, qw);
        List<Myorder> list = iPage.getRecords();

        if (list==null||list.isEmpty()){
            System.out.println("没有数据");
            return list;
        }else {

         return list;
        }

    }


}
